/*!
 * ARES
 * http://www.yitong.com.cn
 *
 * Date: 2014-09-20
 */
(function() {
    //返回基本路径
    var getBasePath = window['_getBasePath'];
    //返回URL中的参数
    var getParameter = window['_getParameter'];
    //参数配置
    var config = {
        debug : window['_debug'],
        version : getParameter('v') || 1,
        lang : getParameter('lang') || 'zh-CN',
        fwPath: window['_fwPath']
    };
    
    var basePath = getBasePath();
    //
    if (config.debug) {
        var vars = ['<script type="text/javascript" src="' + config.fwPath, config.version + '" charset="utf-8"></script>'];
        //框架脚本
        var html = '{0}/plugins/seajs/sea.js?v={1}';
        html += '{0}/plugins/juicer/juicer.js?v={1}';
        html += '{0}/plugins/jquery/jquery.core.js?v={1}';
        html += '{0}/plugins/jquery/jquery.placeholder.js?v={1}';
        html += '{0}/plugins/jquery/jquery.serializeobject.js?v={1}';
        // html += '{0}/plugins/jquery/jquery.scrollIntoView.js?v={1}';
        // html += '{0}/plugins/jquery/jquery.touchSwipe.js?v={1}';
        html += '{0}/plugins/jquery/jquery.mousewheel.js?v={1}';
        html += '{0}/plugins/jquery/jquery.storage.js?v={1}';
        html += '{0}/core/Fw.js?v={1}';
        html += '{0}/core/Fw.override.js?v={1}';
        html += '{0}/util/String.js?v={1}';
        html += '{0}/util/Date.js?v={1}';
        html += '{0}/util/Logger.js?v={1}';
        html += '{0}/util/Observable.js?v={1}';
        html += '{0}/util/DelayedTask.js?v={1}';
        html += '{0}/util/JSON.js?v={1}';
        html += '{0}/core/Component.js?v={1}';
        html += '{0}/util/LoadMask.js?v={1}';
        html += '{0}/util/Ajax.js?v={1}';
        html += '{0}/util/Format.js?v={1}';
        html += '{0}/util/VTypes.js?v={1}';
        //
        html = html.replace(/\{(\d+)\}/g, function(m, i) {
            return vars[i];
        });
        document.write(html);
    }

    //
    var completed = function() {
        var documentMode = document.documentMode;
        if (!Fw.isIE || documentMode > 8 || (documentMode < 9 && document.readyState === 'complete')) {
            try {
                //框架配置
                Fw.getBasePath = getBasePath;
                Fw.getParameter = getParameter;
                Fw.apply(Fw, config);
                //SEA配置
                seajs.config({
                    base : basePath,
                    paths: {
                        echarts: '{fwPath}/widget/echart',
                        security: '{fwPath}/widget/security/core'
                    },
                    alias : {
                        'Fw.Affix' : '{fwPath}/widget/Affix',
                        'Fw.Alert' : '{fwPath}/widget/Alert',
                        'Fw.Button' : '{fwPath}/widget/Button',
                        'Fw.Carousel' : '{fwPath}/widget/Carousel',
                        'Fw.Collapse' : '{fwPath}/widget/Collapse',
                        'Fw.Dropdown' : '{fwPath}/widget/Dropdown',
                        'Fw.Window' : '{fwPath}/widget/Modal',
                        'Fw.Popover' : '{fwPath}/widget/Popover',
                        'Fw.Scrollspy' : '{fwPath}/widget/Scrollspy',
                        'Fw.Tab' : '{fwPath}/widget/Tab',
                        'Fw.Tooltip' : '{fwPath}/widget/Tooltip',
                        'Fw.ListView' : '{fwPath}/widget/ListView',
                        'Fw.Transition' : '{fwPath}/widget/Transition',
                        'Fw.Datetimepicker' : '{fwPath}/widget/Datetimepicker',
                        'Fw.Rotate' : '{fwPath}/widget/Rotate',
                        'Fw.FormValidation' : '{fwPath}/widget/FormValidation',
                        'Fw.FollowLayer' : '{fwPath}/widget/FollowLayer',
                        'Fw.Upload' : '{fwPath}/widget/Upload',
                        'Fw.Jcrop' : '{fwPath}/widget/Jcrop',
                        //
                        'Fw.Security' : '{fwPath}/widget/security/Security',
                        //App
                        'Fw.app.Session' : '{fwPath}/widget/app/Session'
                    },
                    vars : config,
                    map : [[/^(.*\.(?:css|js))(?:.*)$/i, '$1?v=' + config.version]],
                    charset : 'utf-8'
                });
                //通过SEA引入的基本类库
                Fw.BASE_LIB = ['Fw.Transition', 'Fw.Tooltip', 'Fw.Button', 'Fw.Window', 'Fw.Security'];//
                
                //补充样式（仅针对IE）
                if(Fw.isIE){
                    $('body').addClass('ie' + documentMode);
                }
            
                //自动构建APP脚本
                //每个页面都绑定一个同名JS
                var path = document.location.href;
                var main = location.pathname.replace(/(.+)[\\/]/, '');
                if (main) {
		        	path = path.split('?')[0];
	              	main = path.substring(basePath.length + 1, path.lastIndexOf('.'));
            	}
                else {
                    main = path.split('?')[0].slice(0, path.lastIndexOf('/#')) + '/index';
                }
                //
                Fw.appPath = config.debug ? main : main + '.min';
                var script = document.createElement('script');
                script.type = 'text/javascript';
                //script.text = 'seajs.use(' + Fw.encode(Fw.BASE_LIB) + ', function(){seajs.use("' + (config.debug ? main : main + '.min') + '")});';
                script.text = 'seajs.use({0}, function(){Fw.Logger.debug("加载业务功能（{1}）");seajs.use("{1}")});'.format(
                    Fw.encode(Fw.BASE_LIB),
                    Fw.appPath
                );
                (document.head || document.getElementsByTagName('head')[0]).appendChild(script);
            }
            catch(e) {
                alert(e);
            }
        }
    };
    var addEvent, event;
    if (window.addEventListener) {
        addEvent = window.addEventListener;
        event = 'DOMContentLoaded';
    } 
    else {
        addEvent = document.attachEvent;
        event = 'onreadystatechange';
    }
    addEvent(event, completed, false);
})(); 